package com.nari.ncps.screen.service.impl.dryport;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.dryport.DryportSafetyenvLine;
import com.nari.ncps.screen.mapper.dryport.DryportSafetyenvLineMapper;
import com.nari.ncps.screen.service.dryport.DryportSafetyenvLineService;
import com.nari.ncps.screen.vo.dryport.LineVO;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 营业线路计划情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class DryportSafetyenvLineServiceImpl extends ServiceImpl<DryportSafetyenvLineMapper, DryportSafetyenvLine> implements DryportSafetyenvLineService {

    @Override
    public List<LineVO> getLineStatistics(Integer year, Integer month, Integer day) {
        // 默认获取今日数据
        LocalDate today = LocalDate.now();
        if (year == null) {
            year = today.getYear();
        }
        if (month == null) {
            month = today.getMonthValue();
        }
        if (day == null) {
            day = today.getDayOfMonth();
        }
        
        // 查询指定日期数据，只取一条
        LambdaQueryWrapper<DryportSafetyenvLine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DryportSafetyenvLine::getYear, year);
        queryWrapper.eq(DryportSafetyenvLine::getMonth, month);
        queryWrapper.eq(DryportSafetyenvLine::getDay, day);
        queryWrapper.orderByDesc(DryportSafetyenvLine::getCreateTime);
        queryWrapper.last("LIMIT 1");
        List<DryportSafetyenvLine> entityList = this.list(queryWrapper);
        
        // 转换为VO对象列表
        List<LineVO> result = new ArrayList<>();
        
        for (DryportSafetyenvLine entity : entityList) {
            LineVO vo = new LineVO();

            vo.setRailwayPlanned(entity.getRailwayPlanned());
            vo.setYestMaintAchvQty(entity.getYestMaintAchvQty());
            vo.setYestMaintPlanQty(entity.getYestMaintPlanQty());
            
            result.add(vo);
        }
        
        return result;
    }
}
